def get_brain_figure(g, plot_title=''):
"""
Returns the plotly figure object for vizualizing a 3d brain network.
g: networkX object of brain
"""
# grab the node positions from the graphML file
V = nx.number_of_nodes(g)
attributes = nx.get_node_attributes(g,'attr')
node_positions_3d = pd.DataFrame(columns=['x', 'y', 'z'], index=range(V))
for n in g.nodes_iter():
node_positions_3d.loc[n] = [int((re.findall('\d+', str(attributes[n])))[0]), int((re.findall('\d+', str(attributes[n])))[1]), int((re.findall('\d+', str(attributes[n])))[2])]
# grab edge endpoints
edge_x = []
edge_y = []
edge_z = []
for e in g.edges_iter():
#strippedSource = int(e[0].replace('s', ''))
#strippedTarget = int(e[1].replace('s', ''))
source_pos = node_positions_3d.loc[e[0]]
target_pos = node_positions_3d.loc[e[1]]
edge_x += [source_pos['x'], target_pos['x'], None]
edge_y += [source_pos['y'], target_pos['y'], None]
edge_z += [source_pos['z'], target_pos['z'], None]
# node style
node_trace = Scatter3d(x=node_positions_3d['x'],
y=node_positions_3d['y'],
z=node_positions_3d['z'],
mode='markers',
# name='regions',
marker=Marker(symbol='dot',
size=6,
opacity=0.5,
color='purple'),
# text=[str(r) for r in range(V)],
# text=atlas_data['nodes'],
hoverinfo='text')
# edge style
edge_trace = Scatter3d(x=edge_x,
y=edge_y,
z=edge_z,
mode='lines',
line=Line(color='cyan', width=1),
hoverinfo='none')
# axis style
axis = dict(showbackground=False,
showline=False,
zeroline=False,
showgrid=False,
showticklabels=False)
# overall layout
layout = Layout(title=plot_title,
width=800,
height=900,
showlegend=False,
scene=Scene(xaxis=XAxis(axis),
yaxis=YAxis(axis),
zaxis=ZAxis(axis)),
margin=Margin(t=50),
hovermode='closest',
paper_bgcolor='rgba(1,1,1,1)',
plot_bgcolor='rgb(1,1,1)')
data = Data([node_trace, edge_trace])
fig = Figure(data=data, layout=layout)
return fig